package com.fjrz.bbxwj.main.notice

import android.app.Activity
import android.content.Intent
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.lifecycle.Observer
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.mode.vm.MessageViewModel
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.code
import com.module.lemlin.rxhttp.msg
import kotlinx.android.synthetic.main.activity_message_details.*

class MessageDetailsActivity : ViewModeActivity<MessageViewModel>() {

    companion object {
        private const val EXTRA_KEY_TITLE = "EXTRA_KEY_TITLE"
        private const val EXTRA_KEY_TYPE = "EXTRA_KEY_TYPE"
        private const val EXTRA_KEY_ID = "EXTRA_KEY_ID"

        private const val TYPE_BULLETIN = 1
        private const val TYPE_SYSTEM = 2

        fun openBulletin(activity: Activity, id: Int) {
            val intent: Intent = Intent(activity, MessageDetailsActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, "官方公告")
                putExtra(EXTRA_KEY_TYPE, TYPE_BULLETIN)
                putExtra(EXTRA_KEY_ID, id)
            }
            activity.startActivity(intent)
        }

        fun openSystem(activity: Activity, id: Int) {
            val intent: Intent = Intent(activity, MessageDetailsActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, "系统消息")
                putExtra(EXTRA_KEY_TYPE, TYPE_SYSTEM)
                putExtra(EXTRA_KEY_ID, id)
            }
            activity.startActivity(intent)
        }
    }

    private val mTitle: String
        get() = intent?.getStringExtra(EXTRA_KEY_TITLE) ?: ""

    private val mType: Int
        get() = intent?.getIntExtra(EXTRA_KEY_TYPE, 0) ?: 0

    private val mId: Int
        get() = intent?.getIntExtra(EXTRA_KEY_ID, 0) ?: 0


    override fun viewModel(): MessageViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_message_details

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, view = flMessageDetailsBar)
    }

    override fun initView() {

        ivMessageDetailsBack.setOnClickListener { this.finish() }

        tvMessageDetailsTitle.text = mTitle

        wvMessageDetailsWeb.webViewClient = WebViewClient()
        wvMessageDetailsWeb.settings.apply {
            //支持缩放，默认为true。
            setSupportZoom(false)
            //调整图片至适合webview的大小
            useWideViewPort = true
            // 缩放至屏幕的大小
            loadWithOverviewMode = true
            //设置默认编码
            defaultTextEncodingName = "utf-8"
            //设置自动加载图片
            loadsImagesAutomatically = true
            //允许访问文件
            allowFileAccess = true
            //开启javascript
            javaScriptEnabled = true
            //支持通过JS打开新窗口
            javaScriptCanOpenWindowsAutomatically = true
            //支持内容重新布局
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
            //关闭webview中缓存
            cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK

        }
    }

    override fun initData() {
        if (mType == 1 && mId != 0) {
            viewModel.messageAnnouncementsDetails(mId)
        } else if (mType == 2 && mId != 0) {
            viewModel.messageSystemDetails(1)
        }
    }

    override fun initViewMode() {
        viewModel.messageAnnouncementsDetailsLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE)
                toast(response.throwable?.code,response.throwable?.msg)
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.let {
                wvMessageDetailsWeb.loadData(it.content, ("text/html; charset=UTF-8"), (null))
            }
        })
        viewModel.messageSystemDetailsLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE)
                toast(response.throwable?.code,response.throwable?.msg)
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.let {
                wvMessageDetailsWeb.loadData(it.content, ("text/html; charset=UTF-8"), (null))
            }
        })
    }

}